/**
 * 
 */
package ac.svn.accesscontrol.controller;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import ac.notification.event.INotificationListener;
import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.AccessFile;
import ac.svn.accesscontrol.store.Dictionary;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.ui.window.ConfigFileSelectorDialog;

/**
 * @author Enigmanic
 *
 */
public class AccessFileLoader implements INotificationListener,ISVNIdentifier {

	private ConfigFileSelectorDialog dialog;
	public AccessFileLoader()
	{
		dialog = new ConfigFileSelectorDialog();
		NotificationRegistrar.registerListener(this);
	}
	
	/* (non-Javadoc)
	 * @see ac.notification.event.INotificationListener#declaredSubscriptionSet()
	 */
	@Override
	public Set<String> declaredSubscriptionSet() {
		Set<String> subcriptionSet = new HashSet<String>();
		subcriptionSet.add(CMD_OPEN_FILE);
		return subcriptionSet;
	}

	/* (non-Javadoc)
	 * @see ac.notification.event.INotificationListener#receiveEvent(ac.notification.event.NotificationEvent)
	 */
	@Override
	public void receiveEvent(NotificationEvent notificationEvent) {
		if(notificationEvent.getPropertyIdentifier().contains(CMD_OPEN_FILE))
		{
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					chooseFile();					
				}
			});
			
		}
	}
	
	private void chooseFile()
	{
		int selectedIndex = dialog.showConfigSelectionDialog();
		//Dispatch event to open/load file.
		NotificationEvent event = null;
		System.out.println(String.valueOf(selectedIndex));
		if(ConfigFileSelectorDialog.OK_OPTION == selectedIndex)
		{
			AccessFile file = dialog.getSelectedFile();
			if(null != file)
			{
				Dictionary.getDictionary().setLastAccessFile(file);
				Dictionary.getDictionary().addAccessFile(file);
				if(dialog.isSetDefault()){
					Dictionary.getDictionary().setAlwaysOpen(true);
				}
				event = new NotificationEvent(ISVNIdentifier.CMD_PROCESS_FILE);
				event.setPropertyValue(file.getAccessFileLocation());
				NotificationRegistrar.dispatchNotification(event)	;
				return;
			}
			else
			{
				int selection = JOptionPane.showConfirmDialog(null, "<HTML>You've not selected any access file, make selection from the 'Recent Files' list Or Browse a file!" +
						"<br>Press Cancel to exit program.","No Selection Made!",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE );
				if(JOptionPane.OK_OPTION == selection)
				{
					event = new NotificationEvent(ISVNIdentifier.CMD_OPEN_FILE);
					NotificationRegistrar.dispatchNotification(event)	;
					return;
				}
				else
				{
					System.exit(0);
				}				
			}		
		}
		else
		{
			JOptionPane.showMessageDialog(null, "No access file was selected, Go to Action->Open for opening a access file!","No Selection!",JOptionPane.WARNING_MESSAGE);
			//System.exit(0);
		}
		
	}

}
